package cn.watson.basic.controller;


import cn.watson.basic.dto.SmsCodeDto;
import cn.watson.basic.exception.BusinessException;
import cn.watson.basic.service.IVerifyCodeService;
import cn.watson.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
* @Title:
* @Description: 生成图像验证码的接口类
* @Author: ZhangRui
* @Version: 1.0
* @Date:  2022/7/4 19:56
* @Parameters:
* @Return
*/
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
    * @Title: graph
    * @Description: 生成图像验证码的接口
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/7/5 23:56
    * @Parameters: [key]
    * @Return cn.watson.basic.utils.AjaxResult
    */
    @GetMapping("/image/{key}")
    public AjaxResult graph(@PathVariable String key){
        try {
            String base64Str = verifyCodeService.getImageCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage("系统繁忙！");
        }
    }


    /**
    * @Title: graph
    * @Description: 接收发送图形验证码的值，用于做比较
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/7/6 15:54
    * @Parameters: [smsCodeDto]
    * @Return cn.watson.basic.utils.AjaxResult
    */
    @PostMapping("/smsCode")         // 公共层里面 自定义的临时对象
    public AjaxResult smsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            // 调用业务层发送验证
            verifyCodeService.smsCode(smsCodeDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage("系统繁忙！");
        }
    }


}
